home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-06-17 | 6.2 KB | 285 lines | [TEXT/CWIE] |
- /*****************************************************************************/
- //
- // Utility.c
- //
- // Various utility functions for use in the recursive shell program.
- //
- // Version 1.0.1
- //
- // Created: 13 May 1996
- // Modified: 17 June 1996
- //
- /*****************************************************************************/
-
- #include "Utility.h"
-
-
- // Prototypes
-
-
- // BusyCursor
- //
- // Changes the cursor into the watch.
- void BusyCursor( void )
- {
- CursHandle myCursorHandle;
-
- myCursorHandle = GetCursor( watchCursor );
- if ( myCursorHandle != nil )
- SetCursor( *myCursorHandle );
- }
-
-
- //
- // SysSevenOrBetter
- //
- // Returns true if the current system version is 7.0 or higher.
- // Returns false otherwise.
- //
- Boolean SysSevenOrBetter ( void )
- {
- SysEnvRec myEnviron;
- OSErr myErr;
-
- // Oddly enough, Apple discourages the use of Gestalt to determine
- // current operating system version. So, we use SysEnvirons instead:
-
- myErr = SysEnvirons( curSysEnvVers, &myEnviron );
- if ( myErr != noErr )
- return false;
-
- if ( myEnviron.systemVersion >= 0x0700 )
- return true;
- else
- return false;
- }
-
-
- //
- // IsAFolder
- //
- // Returns true if theSpec is a folder, false otherwise.
- // Code taken from Andy Duncan's Scruncher code.
- //
- Boolean IsAFolder(FSSpec* theSpec)
- {
- CInfoPBRec pb;
- OSErr err;
-
- pb.hFileInfo.ioCompletion = (IOCompletionUPP) NULL;
- pb.hFileInfo.ioNamePtr = theSpec->name;
- pb.hFileInfo.ioVRefNum = theSpec->vRefNum;
- pb.hFileInfo.ioFDirIndex = 0; // Find out about this file.
- pb.hFileInfo.ioDirID = theSpec->parID;
-
- err = PBGetCatInfo(&pb, false);
- if (err != noErr)
- return false;
- else
- return ((pb.hFileInfo.ioFlAttrib & (1 << 4)) != 0);
- }
-
-
- //
- // AppendColon
- //
- // Adds a colon to the end of the Pascal string dirName.
- //
- void AppendColon( Str255 dirName )
- {
- short length;
-
- length = dirName[0];
- length++;
- dirName[0] = length;
- dirName[length] = ':';
- }
-
-
- //
- // CopyPString
- //
- // Copies all of Pascal string copyFrom into copyTo.
- //
- void CopyPString( Str255 copyFrom, Str255 copyTo )
- {
- short i;
-
- for ( i = 0; i <= copyFrom[0]; i++ )
- copyTo[i] = copyFrom[i];
- }
-
-
- //
- // ConcatPStrings
- //
- // Contaenates Pascal strings aStr and bStr, placing the result into destStr.
- //
- void ConcatPStrings( Str255 aStr, Str255 bStr, Str255 destStr )
- {
- short i, j, length, aLength, bLength;
- short aCount, bCount;
- Str255 firstString;
- Str255 secondString;
-
- CopyPString( aStr, firstString );
- CopyPString( bStr, secondString );
-
- aLength = firstString[0];
- bLength = secondString[0];
- length = aLength + bLength;
-
- aCount = 1;
- bCount = 1;
-
- if ( length < 256 )
- {
- destStr[0] = length;
-
- for ( i = 1; i <= aLength; i++ )
- destStr[i] = firstString[i];
-
- for ( j = 1; i <= length; i++, j++ )
- destStr[i] = secondString[j];
-
- }
- else
- {
- destStr[0] = aLength;
-
- for ( i = 1; i <= aLength; i++ )
- destStr[i] = firstString[i];
- }
- }
-
-
- //
- // GetFullPath
- //
- // Puts the full path name of theSpec into pathName. Based on sample code
- // taken from New Inside Macintosh: Files.
- //
- void GetFullPath( FSSpec* theSpec, Str255 pathName )
- {
- CInfoPBRec pb;
- Str255 dirName, fullPath, fileName;
- OSErr err;
- Boolean done = false;
-
- fullPath[0] = '\0';
- dirName[0] = '\0';
-
- CopyPString( theSpec->name, fileName );
- if ( ( IsAFolder( theSpec ) ) && ( theSpec->name[0] != '\0' ) )
- AppendColon( fileName );
-
- pb.hFileInfo.ioNamePtr = dirName;
- pb.hFileInfo.ioVRefNum = theSpec->vRefNum;
- pb.hFileInfo.ioFDirIndex = -1; // Get info about a directory
- pb.dirInfo.ioDrParID = theSpec->parID;
-
- while ( !done )
- {
- // Change ID of directory we're getting info for to parent of one
- // we just got info for:
- pb.dirInfo.ioDrDirID = pb.dirInfo.ioDrParID;
- // Get info for new (parent) directory:
- err = PBGetCatInfo( &pb, false );
-
- if ( err != noErr )
- done = true;
- else
- {
- // Add a colon to the end of the name of the new (parent) directory:
- AppendColon( dirName );
- // Put the name of the directory before the full pathname:
- ConcatPStrings( dirName, fullPath, fullPath );
-
- if ( pb.dirInfo.ioDrDirID == fsRtDirID )
- done = true;
- }
- }
- ConcatPStrings( fullPath, fileName, pathName );
- }
-
-
- //
- // ResolveOSErr
- //
- // Takes those evil OSErr numbers (or at least the ones we're likely to
- // come across) and puts them in slightly more human-readable form.
- //
- void ResolveOSErr( OSErr inErr, Str255 returnValue )
- {
- switch ( inErr )
- {
- case noErr:
- CopyPString( "\pERROR: No error", returnValue );
- break;
- case fBsyErr:
- CopyPString( "\pERROR: File busy, in use, or folder not empty", returnValue );
- break;
- case afpObjectTypeErr:
- CopyPString( "\pERROR: A directory exists with that name", returnValue );
- break;
- case dskFulErr:
- CopyPString( "\pERROR: Disk is full", returnValue );
- break;
- case dirFulErr:
- CopyPString( "\pERROR: File directory is full", returnValue );
- break;
- case tmfoErr:
- CopyPString( "\pERROR: Too many files open", returnValue );
- break;
- case fnOpnErr:
- CopyPString( "\pERROR: File not open", returnValue );
- break;
- case rfNumErr:
- CopyPString( "\pERROR: Bad File Ref number", returnValue );
- break;
- case wPrErr:
- case vLckdErr:
- CopyPString( "\pERROR: Disk is locked", returnValue );
- break;
- case permErr:
- case fLckdErr:
- CopyPString( "\pERROR: File is locked", returnValue );
- break;
- case wrPermErr:
- CopyPString( "\pERROR: Write access denied", returnValue );
- break;
- case opWrErr:
- CopyPString( "\pERROR: File is already open", returnValue );
- break;
- case nsvErr:
- CopyPString( "\pERROR: No such volume", returnValue );
- break;
- case ioErr:
- CopyPString( "\pERROR: I/O error", returnValue );
- break;
- case bdNamErr:
- CopyPString( "\pERROR: Bad filename", returnValue );
- break;
- case fnfErr:
- case dirNFErr:
- CopyPString( "\pERROR: Directory not found or incomplete pathname", returnValue );
- break;
- case dupFNErr:
- CopyPString( "\pERROR: Duplicate filename and version", returnValue );
- break;
- case afpAccessDenied:
- CopyPString( "\pERROR: User does not have the correct access", returnValue );
- break;
- case paramErr:
- CopyPString( "\pERROR: Negative count (-50)", returnValue );
- break;
- case posErr:
- CopyPString( "\pERROR: Attempt to position mark before start of file (-40)", returnValue );
- break;
- default:
- CopyPString( "\pERROR: Unknown Error", returnValue );
- break;
- }
- }
-
-